using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEditor.Sprites;
using System.Collections.Generic;
using System.Linq;

public class CreateFont : EditorWindow
{

    Texture2D tex;
    string fontName;
    string fontPath;

    void OnGUI()
    {
        GUILayout.BeginVertical();

        GUILayout.BeginHorizontal();
        GUILayout.Label("字体图片：");
        tex = (Texture2D)EditorGUILayout.ObjectField(tex, typeof(Texture2D), true);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("字体名称：");
        fontName = EditorGUILayout.TextField(fontName);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button(string.IsNullOrEmpty(fontPath) ? "选择路径" : fontPath))
        {
            fontPath = EditorUtility.OpenFolderPanel("字体路径", Application.dataPath, "");
            if (string.IsNullOrEmpty(fontPath))
            {
                Debug.Log("取消选择路径");
            }
            else
            {
                fontPath = fontPath.Replace(Application.dataPath, "") + "/";
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("创建"))
        {
            Create();
        }
        GUILayout.EndHorizontal();
        // tips
        // GUILayout.BeginHorizontal();
        GUILayout.Label("tips: 1.选择的图片必须是sprite类型，且图片名称为字体名称，字体名称不能重复。");
        GUILayout.Label("        2.图片的Sprite mode 必须设置为Multiple。");
        // GUILayout.EndHorizontal();
        GUILayout.EndVertical();
    }

    void Create()
    {
        if (tex == null)
        {
            Debug.LogWarning("创建失败，图片为空！");
            return;
        }

        if (string.IsNullOrEmpty(fontPath))
        {
            Debug.LogWarning("字体路径为空！");
            return;
        }
        if (fontName == null)
        {
            Debug.LogWarning("创建失败，字体名称为空！");
            return;
        }
        else
        {
            if (File.Exists(Application.dataPath + fontPath + fontName + ".fontsettings"))
            {
                Debug.LogError("创建失败，已存在同名字体文件");
                return;
            }
            if (File.Exists(Application.dataPath + fontPath + fontName + ".mat"))
            {
                Debug.LogError("创建失败，已存在同名字体材质文件");
                return;
            }
        }

        string selectionPath = AssetDatabase.GetAssetPath(tex);
        string selectionExt = Path.GetExtension(selectionPath);
        if (selectionExt.Length == 0)
        {
            Debug.LogError("创建失败！");
            return;
        }

        string fontPathName = fontPath + fontName + ".fontsettings";
        string matPathName = fontPath + fontName + ".mat";
        float lineSpace = 0.1f;
        AssetImporter assetImporter = AssetImporter.GetAtPath(selectionPath);
        string assetBundleName = assetImporter.assetBundleName;
        //string loadpath = "c:/simpleframework/" + assetBundleName;
        string loadpath = Application.dataPath + "/Resources/Font/" + assetBundleName;

        var assetName = selectionPath.Replace("Assets/Resources/", "").Replace(".png", "");
        Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(selectionPath).OfType<Sprite>().ToArray();
        // Sprite[] sprites = Resources.LoadAll<Sprite>(assetName);
        // Debug.Log(sprites.Length);
        if (sprites.Length > 0)
        {
            Material mat = new Material(Shader.Find("GUI/Text Shader"));
            mat.SetTexture("_MainTex", tex);
            Font m_myFont = new Font();
            m_myFont.material = mat;
            CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];
            for (int i = 0; i < sprites.Length; i++)
            {
                if (sprites[i].rect.height > lineSpace)
                {
                    lineSpace = sprites[i].rect.height;
                }
            }
            for (int i = 0; i < sprites.Length; i++)
            {
                Sprite spr = sprites[i];
                CharacterInfo info = new CharacterInfo();
                try
                {
                    // sub spr name in last str
                    var sub = spr.name.Substring(spr.name.Length - 1, 1);
                    int num = (int)sub[0];
                    // num convert to ascii
                    var ascii = System.Convert.ToInt32(num);
                    info.index = ascii;
                }
                catch
                {
                    Debug.LogError("创建失败，Sprite名称错误！");
                    return;
                }
                Rect rect = spr.rect;
                float pivot = spr.pivot.y / rect.height - 0.5f;
                if (pivot > 0)
                {
                    pivot = -lineSpace / 2 - spr.pivot.y;
                }
                else if (pivot < 0)
                {
                    pivot = -lineSpace / 2 + rect.height - spr.pivot.y;
                }
                else
                {
                    pivot = -lineSpace / 2;
                }
                int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);
                info.uvBottomLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y) / tex.height);
                info.uvBottomRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y) / tex.height);
                info.uvTopLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y + rect.height) / tex.height);
                info.uvTopRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y + rect.height) / tex.height);
                info.minX = 0;
                info.minY = -(int)rect.height - offsetY;
                info.maxX = (int)rect.width;
                info.maxY = -offsetY;
                info.advance = (int)rect.width;
                characterInfo[i] = info;
            }
            AssetDatabase.CreateAsset(mat, "Assets" + matPathName);
            AssetDatabase.CreateAsset(m_myFont, "Assets" + fontPathName);
            m_myFont.characterInfo = characterInfo;
            EditorUtility.SetDirty(m_myFont);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();//刷新资源
            Debug.Log("创建字体成功");

        }
        else
        {
            Debug.LogError("图集错误！");
        }
    }
}